package ar.republicagroup.gestion.view;

import ar.republicagroup.gestion.entities.Cliente;
import ar.republicagroup.gestion.services.ClienteServices;
import ar.republicagroup.gestion.util.SpringUtils;
import ar.republicagroup.gestion.util.Validar;
import ar.republicagroup.gestion.util.messages.UIMessages;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.apache.log4j.Logger;

public class modalPanelEditCliente extends javax.swing.JDialog {

    ClienteServices clienteServices = (ClienteServices)SpringUtils.getBean("clienteServices");
    
    
    private int id_cliente;
    private static final int ULTIMO = 1;
    private static final int NO_ULTIMO = 0;
    private boolean fromAnotherFrame;
    
    List<JTextField> txtList = new ArrayList<>();
    
    private static Logger log = Logger.getLogger(modalPanelEditCliente.class);
    
    public modalPanelEditCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/moneybox.png")).getImage());
        cargarListaDeJTextFieldObligatorios();
    }

    public int getId_cliente() {
        return id_cliente;
    }

    public void setId_cliente(int id_cliente) {
        this.id_cliente = id_cliente;
    }

    public boolean isFromAnotherFrame() {
        return fromAnotherFrame;
    }

    
    public void setFromAnotherFrame(boolean fromAnotherFrame) {
        this.fromAnotherFrame = fromAnotherFrame;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelContenedor = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCelular = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtComCAP = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtComVC = new javax.swing.JTextField();
        btnActualizar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblErrorCliente = new javax.swing.JLabel();
        lblErrorTelefono = new javax.swing.JLabel();
        lblErrorDireccion = new javax.swing.JLabel();
        lblErrorCelular = new javax.swing.JLabel();
        lblErrorComBsAs = new javax.swing.JLabel();
        lblErrorComInt = new javax.swing.JLabel();
        lblErrorEmail = new javax.swing.JLabel();
        txtNextel = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        chkComisionRechazo = new javax.swing.JCheckBox();
        jLabel9 = new javax.swing.JLabel();
        txtComDepo = new javax.swing.JTextField();
        lblErrorComDepo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("..: Modificar datos de Cliente :...");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        panelContenedor.setBorder(javax.swing.BorderFactory.createTitledBorder("Editar cliente"));
        panelContenedor.setPreferredSize(new java.awt.Dimension(530, 301));

        jLabel1.setText("Cliente");

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("ar/republicagroup/gestion/view/properties/modalPanelAddCliente_es"); // NOI18N
        txtCliente.setName(bundle.getString("txtCliente")); // NOI18N
        txtCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtClienteKeyPressed(evt);
            }
        });

        jLabel2.setText("Teléfono");

        txtTelefono.setName(bundle.getString("txtTelefono")); // NOI18N
        txtTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtTelefonoKeyPressed(evt);
            }
        });

        jLabel3.setText("Dirección");

        txtDireccion.setName(bundle.getString("txtDireccion")); // NOI18N
        txtDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDireccionKeyPressed(evt);
            }
        });

        jLabel4.setText("Email");

        txtEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtEmailKeyPressed(evt);
            }
        });

        jLabel5.setText("Celular");

        txtCelular.setName(bundle.getString("txtCelular")); // NOI18N
        txtCelular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCelularKeyPressed(evt);
            }
        });

        jLabel6.setText("Com.CAP");

        txtComCAP.setName(bundle.getString("txtComBsAs")); // NOI18N
        txtComCAP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComCAPKeyPressed(evt);
            }
        });

        jLabel7.setText("Com.VC");

        txtComVC.setName(bundle.getString("txtComInt")); // NOI18N
        txtComVC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComVCKeyPressed(evt);
            }
        });

        btnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/update.png"))); // NOI18N
        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblErrorCliente.setForeground(new java.awt.Color(255, 0, 0));
        java.util.ResourceBundle bundle1 = java.util.ResourceBundle.getBundle("ar/republicagroup/gestion/util/messages/gestionAppErrorMessages"); // NOI18N
        lblErrorCliente.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorTelefono.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorTelefono.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorDireccion.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorDireccion.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorCelular.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorCelular.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorComBsAs.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorComBsAs.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorComInt.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorComInt.setText(bundle1.getString("requiredField")); // NOI18N

        lblErrorEmail.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorEmail.setText(bundle1.getString("email")); // NOI18N

        jLabel8.setText("Nextel");

        chkComisionRechazo.setSelected(true);
        chkComisionRechazo.setText("Comisión de rechazo");

        jLabel9.setText("Com.Depósito");

        txtComDepo.setName("Com.Depósito"); // NOI18N
        txtComDepo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtComDepoActionPerformed(evt);
            }
        });
        txtComDepo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComDepoKeyPressed(evt);
            }
        });

        lblErrorComDepo.setForeground(new java.awt.Color(255, 0, 0));
        lblErrorComDepo.setText(bundle1.getString("requiredField")); // NOI18N

        javax.swing.GroupLayout panelContenedorLayout = new javax.swing.GroupLayout(panelContenedor);
        panelContenedor.setLayout(panelContenedorLayout);
        panelContenedorLayout.setHorizontalGroup(
            panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContenedorLayout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelContenedorLayout.createSequentialGroup()
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelContenedorLayout.createSequentialGroup()
                                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtDireccion)
                                    .addComponent(txtTelefono)
                                    .addComponent(txtCliente)
                                    .addComponent(txtCelular)
                                    .addComponent(txtEmail)
                                    .addComponent(txtNextel))
                                .addGap(18, 18, 18)
                                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblErrorCelular)
                                    .addComponent(lblErrorEmail)
                                    .addComponent(lblErrorDireccion)
                                    .addComponent(lblErrorTelefono)
                                    .addComponent(lblErrorCliente)))
                            .addGroup(panelContenedorLayout.createSequentialGroup()
                                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(panelContenedorLayout.createSequentialGroup()
                                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(txtComCAP, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                            .addComponent(txtComVC, javax.swing.GroupLayout.Alignment.LEADING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblErrorComBsAs)
                                            .addComponent(lblErrorComInt)))
                                    .addGroup(panelContenedorLayout.createSequentialGroup()
                                        .addComponent(btnActualizar)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnCancelar)))
                                .addGap(0, 65, Short.MAX_VALUE)))
                        .addGap(119, 119, 119))
                    .addGroup(panelContenedorLayout.createSequentialGroup()
                        .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkComisionRechazo)
                            .addGroup(panelContenedorLayout.createSequentialGroup()
                                .addComponent(txtComDepo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblErrorComDepo)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        panelContenedorLayout.setVerticalGroup(
            panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContenedorLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorTelefono))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorDireccion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lblErrorCelular))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNextel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(lblErrorEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtComCAP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorComBsAs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtComVC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorComInt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtComDepo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblErrorComDepo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(chkComisionRechazo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnActualizar)
                    .addComponent(btnCancelar))
                .addGap(20, 20, 20))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelContenedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtClienteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtClienteKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtCliente.getText(), txtCliente, lblErrorCliente, txtTelefono,NO_ULTIMO);
        }
    }//GEN-LAST:event_txtClienteKeyPressed

    private void txtTelefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTelefonoKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtTelefono.getText(), txtTelefono, lblErrorTelefono, txtDireccion,NO_ULTIMO);
        }
    }//GEN-LAST:event_txtTelefonoKeyPressed

    private void txtDireccionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtDireccion.getText(), txtDireccion, lblErrorDireccion, txtCelular,NO_ULTIMO);
        }
    }//GEN-LAST:event_txtDireccionKeyPressed

    private void txtEmailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtEmailKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            if(!Validar.esVacio(txtEmail.getText()) && !Validar.validarVacio(txtEmail.getText())){
                if(!Validar.validarEmail(txtEmail.getText())){
                    lblErrorEmail.setVisible(true);
                    txtEmail.requestFocus();
                }else{
                    lblErrorEmail.setVisible(false);
                    txtComCAP.requestFocus();
                }
            }else{
                lblErrorEmail.setVisible(false);
                txtComCAP.requestFocus();
            }
        }
    }//GEN-LAST:event_txtEmailKeyPressed

    private void txtCelularKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCelularKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtCelular.getText(), txtCelular, lblErrorCelular, txtEmail,NO_ULTIMO);
        }
    }//GEN-LAST:event_txtCelularKeyPressed

    private void txtComCAPKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComCAPKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtComCAP.getText(), txtComCAP, lblErrorComBsAs, txtComVC,NO_ULTIMO);
        }
    }//GEN-LAST:event_txtComCAPKeyPressed

    private void txtComVCKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComVCKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            validarSiCajaDeTextoEsVacia(txtComVC.getText(), txtComVC, lblErrorComInt, null,ULTIMO);
        }
    }//GEN-LAST:event_txtComVCKeyPressed

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
        if(esValidoElFormulario()){
            actualizarCliente();
        }        
    }//GEN-LAST:event_btnActualizarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
       valoresPorDefecto();
        
       Cliente cliente = clienteServices.get(getId_cliente());
       txtCliente.setText(cliente.getNombre());
       txtTelefono.setText(cliente.getTelefono());
       txtDireccion.setText(cliente.getDireccion());
       txtCelular.setText(cliente.getCelular());
       txtNextel.setText(cliente.getNextel());
       txtEmail.setText(cliente.getEmail());
       txtComCAP.setText(cliente.getComisionBa().toString());
       txtComVC.setText(cliente.getComisionInterior().toString());
       txtComDepo.setText(cliente.getComisionDeposito().toString());
       
       if(cliente.getComisionRechazo().equals("N")){
           chkComisionRechazo.setSelected(false);
       }
       
       /*Si es llamado de un frame que no sea el frmCliente*/
       if(fromAnotherFrame){
           txtCliente.setEnabled(false);
           txtTelefono.setEnabled(false);
           txtDireccion.setEnabled(false);
           txtCelular.setEnabled(false);
           txtNextel.setEnabled(false);
           txtEmail.setEnabled(false);
           txtComCAP.setEnabled(false);           
           txtComVC.setEnabled(false);
           chkComisionRechazo.setEnabled(false);
           txtComDepo.setText(cliente.getComisionDeposito().toString());
           txtComDepo.selectAll();
           txtComDepo.requestFocus();
       }
       
    }//GEN-LAST:event_formWindowOpened

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
           
           if(!fromAnotherFrame){
                log.info("fromAnotherFrame: "+fromAnotherFrame);
                frmCliente.btnActualizar.doClick();
           }else{
               log.info("fromAnotherFrame: "+fromAnotherFrame);
                frmAddDepositoEfectivo.btnActualizar.doClick();
                
           }
     
    }//GEN-LAST:event_formWindowClosed

    private void txtComDepoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtComDepoActionPerformed
       
    }//GEN-LAST:event_txtComDepoActionPerformed

    private void txtComDepoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComDepoKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            if(esValidoElFormulario()){
                actualizarCliente();
            }
        }
    }//GEN-LAST:event_txtComDepoKeyPressed

    
    public void actualizarCliente(){
        
        Cliente cliente = clienteServices.get(getId_cliente());
        String tempComBsAs = Validar.cambiarComaPorPuntoEnCadena(txtComCAP.getText());
        String tempComInt  = Validar.cambiarComaPorPuntoEnCadena(txtComVC.getText());
        String tempComDep  = Validar.cambiarComaPorPuntoEnCadena(txtComDepo.getText());
        
      if(fromAnotherFrame){  
          cliente.setNombre(txtCliente.getText());
          cliente.setTelefono(txtTelefono.getText());
          cliente.setDireccion(txtDireccion.getText());
          cliente.setCelular(txtCelular.getText());
          cliente.setNextel(txtNextel.getText());
          cliente.setEmail(txtEmail.getText());
          cliente.setComisionBa(BigDecimal.valueOf(Double.parseDouble(tempComBsAs)));
          cliente.setComisionInterior(BigDecimal.valueOf(Double.parseDouble(tempComInt)));
          cliente.setComisionDeposito(BigDecimal.valueOf(Double.parseDouble(tempComDep)));    
      } else {
          cliente.setComisionDeposito(BigDecimal.valueOf(Double.parseDouble(tempComDep)));    
      }
        
      if(chkComisionRechazo.isSelected()){
                cliente.setComisionRechazo("Y");
      } else {
                cliente.setComisionRechazo("N"); 
      }
        
      String mensaje = clienteServices.update(cliente);

      if(mensaje != null){
          JOptionPane.showMessageDialog(this, mensaje, UIMessages.getInfoMessage("onCompleteUpdate_title"), JOptionPane.INFORMATION_MESSAGE);
          dispose();
      } else {
          JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("onUpdateError"), UIMessages.getErrorMessage("onUpdateError_title"), JOptionPane.ERROR_MESSAGE);
      }
 
    
    }
    

      /* VALIDACIONES */
    
    private boolean esValidoElFormulario(){
        /* VALIDAMOS QUE ESTEN COMPLETADOS TODOS LOS CAMPOS OBLIGATORIOS */
          for(JTextField txtField : txtList){  
              if(Validar.esVacio(txtField.getText()) || Validar.validarVacio(txtField.getText())){
                JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredField") +" : "+txtField.getName(), UIMessages.getErrorMessage("requiredField_title"), JOptionPane.ERROR_MESSAGE);
                txtField.requestFocus();
                return false;
                }
          }
          
          if(!Validar.esUnDatoDouble(txtComCAP.getText())){
              JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("decimal"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
              txtComCAP.selectAll();
              txtComCAP.requestFocus();
              return false;
          }
          
           if(!Validar.esUnDatoDouble(txtComVC.getText())){
              JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("decimal"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
              txtComVC.selectAll();
              txtComVC.requestFocus();
              return false;
          }
           
          if(!Validar.esUnDatoDouble(txtComDepo.getText())){
              JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("decimal"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
              txtComDepo.selectAll();
              txtComDepo.requestFocus();
              return false;
          }
          
          return true;       
    }
    
   
    
   private void valoresPorDefecto(){
        lblErrorCliente.setVisible(false);
        lblErrorTelefono.setVisible(false);
        lblErrorDireccion.setVisible(false);
        lblErrorCelular.setVisible(false);
        lblErrorEmail.setVisible(false);
        lblErrorComBsAs.setVisible(false);
        lblErrorComInt.setVisible(false);
        lblErrorComDepo.setVisible(false);
    } 
   
    private void cargarListaDeJTextFieldObligatorios(){
        txtList.add(txtCliente);
        txtList.add(txtTelefono);
        txtList.add(txtDireccion);
        txtList.add(txtCelular);
        txtList.add(txtComCAP);
        txtList.add(txtComVC);
        txtList.add(txtComDepo);
    }
    
  
    
   private void validarSiCajaDeTextoEsVacia(String texto,JTextField cajita,JLabel labelDeError,JTextField siguiente_focus,int ultimo){
         if(!Validar.esVacio(texto) && !Validar.validarVacio(texto)){
             if(ultimo == 0){   
                siguiente_focus.requestFocus();
                }
                labelDeError.setVisible(false);
         }else{
                cajita.requestFocus();
                labelDeError.setVisible(true);
         }
    }
    

    
    public static void main(String args[]) {
       
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                modalPanelEditCliente dialog = new modalPanelEditCliente(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JCheckBox chkComisionRechazo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblErrorCelular;
    private javax.swing.JLabel lblErrorCliente;
    private javax.swing.JLabel lblErrorComBsAs;
    private javax.swing.JLabel lblErrorComDepo;
    private javax.swing.JLabel lblErrorComInt;
    private javax.swing.JLabel lblErrorDireccion;
    private javax.swing.JLabel lblErrorEmail;
    private javax.swing.JLabel lblErrorTelefono;
    private javax.swing.JPanel panelContenedor;
    private javax.swing.JTextField txtCelular;
    private javax.swing.JTextField txtCliente;
    private javax.swing.JTextField txtComCAP;
    private javax.swing.JTextField txtComDepo;
    private javax.swing.JTextField txtComVC;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtNextel;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables

    
    
}
